{include file="common/head" /}
<div id="node"></div>

<script>
    layui.use(['tree', 'jquery', 'element', 'layer'], function () {
        var tree = layui.tree,
            $ = layui.jquery,
            layer = layui.layer,
            element = layui.element;

        get('#node', render);

    });

    function nodeSave(action, title) {
        layui.layer.open({
            type: 2,
            title: title,
            content: action,
            area: ['330px', '725px'],
        });
    }

    function get(id, call) {
        layui.jquery.post('{:url("Node/index")}', {}, function (e) {
            if (e.status == 400) {
                layer.msg(e.msg);
            } else {
                call(id, e.data);
            }
        }, 'json');
    }

    function render(id, data) {
        layui.tree.render({
            id: 'node',
            elem: id,  //绑定元素
            data: data,
            isJump: false,
            customOperate: true,
            edit: ['add', 'update', 'del'],
            operate: function (obj) {
                var type = obj.type; //得到操作类型：add、edit、del
                var data = obj.data; //得到当前节点的数据
                var elem = obj.elem; //得到当前节点元素

                //Ajax 操作
                var id = data.id; //得到节点索引

                //添加节点
                if (type === 'add') { //增加节点
                    nodeSave('{:url("Node/save")}?type=add&id=' + id,'添加节点');

                    //修改节点
                } else if (type === 'update') { //修改节点
                    nodeSave('{:url("Node/save")}?type=update&id=' + id,'修改节点');

                    //删除节点
                } else if (type === 'del') { //删除节点
                    layui.layer.open({
                        content: '确定删除该节点?',
                        yes: function(index, layero){
                            layui.jquery.post("{:url('Node/del')}",{id:id},function(e){
                                layer.msg(e.msg);
                                if (e.status == 200) {
                                    window.setTimeout(function () {
                                        reload();
                                        layer.close(index); //如果设定了yes回调，需进行手工关闭
                                    }, 500);
                                }
                            });
                        }
                    });
                }
            },
        });
    }

    function reload() {
        get('#node', render);
    }
</script>
{include file="common/foot" /}